package com.boco.raptor.subsystem.attempx.actionHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.boco.common.util.debug.LogHome;
import com.boco.common.util.except.UserException;
import com.boco.raptor.common.service.IServiceActionContext;
import com.boco.raptor.common.service.ServiceHelper;
import com.boco.raptor.common.service.impl.ServiceActionContext;
import com.boco.raptor.drm.core.dto.DrmEntityFactory;
import com.boco.raptor.drm.core.dto.DrmSingleClassQuery;
import com.boco.raptor.drm.core.dto.IDrmDataObject;
import com.boco.raptor.drm.core.dto.IDrmLabelValue;
import com.boco.raptor.drm.core.dto.IDrmQueryContext;
import com.boco.raptor.drm.core.dto.IDrmQueryResultSet;
import com.boco.raptor.drm.core.dto.IDrmQueryRow;
import com.boco.raptor.drm.core.dto.IDrmRelatedIdValue;
import com.boco.raptor.drm.core.meta.BMClassMeta;
import com.boco.raptor.ext.tnms.adaptor.ServiceBoAdaptor;
import com.boco.transnms.common.dto.base.BoActionContext;
import com.boco.transnms.common.dto.base.BoQueryContext;
import com.boco.transnms.common.dto.base.DataObjectList;
import com.boco.transnms.common.dto.base.GenericDO;
import com.boco.transnms.server.dao.base.AbstractDaoName;
import com.boco.transnms.server.dao.base.DaoHomeFactory;
import com.boco.transnms.server.dao.base.IDataAccessObject;

public class AttempXHelper {
    public static BoQueryContext drmQryCxt2boQryCxt(IDrmQueryContext drmQryCxt){
    	BoQueryContext boQueryContext = new BoQueryContext();
    	boQueryContext.setFetchSize(drmQryCxt.getFetchSize());
    	boQueryContext.setOffset(drmQryCxt.getOffset());
    	boQueryContext.setOrderField(drmQryCxt.getOrderField());
    	boQueryContext.setCountBeforQuery(true);
    	return boQueryContext;
    }
    
    public static IDrmQueryResultSet getLabelValuesBySql(IDrmQueryContext queryContext,
			String sql, GenericDO[] dboTemplates, String lvDbClassId,
			String labelAttrId, String valueAttrId) throws UserException {

		IDrmQueryResultSet rs = null;
		try {
			rs = getGenericDAO().selectDBOs(ServiceBoAdaptor
					.drmQryCxt2boQryCxt(queryContext), sql, dboTemplates);
			List<IDrmQueryRow> rows = rs.getResultSet();

			for (IDrmQueryRow row : rows) {
				IDrmDataObject dbo = row.getResultDbo(lvDbClassId);
				IDrmRelatedIdValue lv = DrmEntityFactory.getInstance()
						.createRelatedIdValue("", dbo.getDbClassId(),
								dbo.getDbClassId(), "CUID",
								dbo.getAttrValue(valueAttrId),
								((String) dbo.getAttrValue(labelAttrId)));
				dbo.getAllAttr().clear();
				dbo.setDboId(null);
				dbo.setAttrValue(labelAttrId, lv);
			}
		} catch (Exception ex) {
			LogHome.getLog().error("", ex);
		}
		return rs;
	}   
    
    public static List<IDrmLabelValue> getDbEnums(String dbClassId){
        List<IDrmLabelValue> enums = new ArrayList();
        try {
            String sql = "select KEY_NUM, KEY_VALUE from " + dbClassId;
            DataObjectList dbos = getGenericDAO().selectDBOs(sql, new Class[]{long.class, String.class});
            for(GenericDO dbo : dbos){
                long value = dbo.getAttrLong("1");
                String label = dbo.getAttrString("2");
                IDrmLabelValue _enum = DrmEntityFactory.getInstance().createLabelValue(label, value);
                enums.add(_enum);
            }

        } catch (Exception ex) {
            LogHome.getLog().error("", ex);
        }
        return enums;
    }
    

    public static String nullObject2String(Object s)
    {
        String str = "";
        try
        {
            str = s.toString();
        }
        catch(Exception e)
        {
            str = "";
        }
        return str;
    }
   
    public static String nullObject2String(Object s, String chr)
    {
        String str = chr;
        try
        {
            str = s.toString();
        }
        catch(Exception e)
        {
            str = chr;
        }
        return str;
    }
    
    private static IDataAccessObject getGenericDAO() {
        return DaoHomeFactory.getInstance().getDAO(AbstractDaoName.GenericDAO);
    }

}
